<template>
  <div>
    <el-dialog
      :title="info.title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-input v-model="input" placeholder="请输入内容"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitHandle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  insertItemCategory,
  updateCategory,
  insertCategory,
} from "@/http/category.js";
export default {
  name: "APP",
  props: ["info", "type"],
  data() {
    return {
      dialogVisible: false,
      input: "",
    };
  },
  methods: {
    handleClose(done) {
      this.dialogVisible = false;
    },
    submitHandle() {
      if (this.type == 1) {
        console.log(this.info.cid, "怎么回事啊");

        this.insertGory({ cid: this.info.cid, name: this.input });
      } else if (this.type == 2) {
        this.updategory({ id: this.info.id, name: this.input });
      } else if (this.type == 3) {
        this.insertFirstCatrgory({ name: this.input });
      }
    },
    insertFirstCatrgory(params) {
      insertCategory(params)
        .then((res) => {
          if (res.data.status == 200) {
            this.$message({
              message: "恭喜你，新增一级类目成功",
              type: "success",
            });
            //隐藏弹框
            this.dialogVisible = false;
            this.$emit("updateViews");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    insertGory(params) {
      insertItemCategory(params)
        .then((res) => {
          if (res.data.status == 200) {
            this.$message({
              message: "恭喜你，新增成功",
              type: "success",
            });
            //隐藏弹框
            this.dialogVisible = false;
            this.$emit("updateViews");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    updategory(params) {
      updateCategory(params)
        .then((res) => {
          if (res.data.status == 200) {
            this.$message({
              message: "恭喜你，新增成功",
              type: "success",
            });
            //隐藏弹框
            this.dialogVisible = false;
            this.$emit("updateViews");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  created() {},
  mounted() {},
};
</script>
<style lang="scss" scoped>
</style>
